/***************************************************************************//**
 * @file CGI/AirportRunway.cpp
 * @author  Marek M. Cel <marekcel@mscsim.org>
 *
 * @section LICENSE
 *
 * Copyright (C) 2014 Marek M. Cel
 *
 * This file is part of MScSim. You can redistribute and modify it under
 * the terms of GNU General Public License as published by the Free Software
 * Foundation; either version 3 of the License, or (at your option) any later
 * version.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
 * for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.
 * 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * Further information about the GNU General Public License can also be found
 * on the world wide web at http://www.gnu.org.
 ******************************************************************************/
#ifndef AIRPORT_RUNWAY_CPP
#define AIRPORT_RUNWAY_CPP
#endif

////////////////////////////////////////////////////////////////////////////////

#include <iostream>

#include <osg/Geode>
#include <osg/Geometry>
#include <osg/Material>
#include <osg/PositionAttitudeTransform>
#include <osg/ShapeDrawable>
#include <osg/Texture2D>

#include <osgDB/ReadFile>

#include <CGI/Airport.h>
#include <CGI/AirportRunway.h>

////////////////////////////////////////////////////////////////////////////////

using namespace std;

////////////////////////////////////////////////////////////////////////////////

AirportRunway::AirportRunway( CGI_Data *data ) :
    m_data ( data )
{
    m_airportRunwayGroup = new osg::Group();
    m_airportRunwayGroup->setName( "AirportRunwayGroup" );

    createRunway();
}

////////////////////////////////////////////////////////////////////////////////

void AirportRunway::createRunway()
{
    // RUNWAY 36
    Airport::createSegment( m_airportRunwayGroup,
                               -1850.0, -Airport::m_halfWidth, -2000.0, Airport::m_halfWidth,
                               "data/textures/terrain/ground_1.png", true );

    // 36 threshold
    Airport::createSegment( m_airportRunwayGroup, -1850.0, -1800.0, 0.0, "data/textures/runway/pa_threshold_br.png" );

    // 36 taxiturn
    Airport::createSegment( m_airportRunwayGroup, -1800.0, -1750.0, 0.0, "data/textures/runway/pa_centerline.png" );

    // 36 designation
    createDigits( 3, 6, -1750.0, -1725.0 );
    Airport::createSegment( m_airportRunwayGroup, -1725.0, -1700.0, 0.0, "data/textures/runway/pa_designation_empty.png" );

    // 36 tc markings
    Airport::createSegment( m_airportRunwayGroup, -1700.0, -1550.0, 0.0, "data/textures/runway/pa_tz_three.png" );
    Airport::createSegment( m_airportRunwayGroup, -1550.0, -1400.0, 0.0, "data/textures/runway/pa_aim.png" );
    Airport::createSegment( m_airportRunwayGroup, -1400.0, -1250.0, 0.0, "data/textures/runway/pa_tz_two.png" );
    Airport::createSegment( m_airportRunwayGroup, -1250.0, -1100.0, 0.0, "data/textures/runway/pa_tz_two.png" );
    Airport::createSegment( m_airportRunwayGroup, -1100.0,  -950.0, 0.0, "data/textures/runway/pa_tz_one.png" );
    Airport::createSegment( m_airportRunwayGroup,  -950.0,  -800.0, 0.0, "data/textures/runway/pa_tz_one.png" );

    // 36 taxiturn
    Airport::createSegment( m_airportRunwayGroup,  -800.0,  -750.0, 0.0, "data/textures/runway/pa_centerline_br.png" );

    Airport::createSegment( m_airportRunwayGroup,
                                0.0, -Airport::m_halfWidth, -750.0, Airport::m_halfWidth,
                                "data/textures/runway/pa_centerline.png", true );

    // RUNWAY 18
    Airport::createSegment( m_airportRunwayGroup,
                                1850.0, Airport::m_halfWidth, 2000.0, -Airport::m_halfWidth,
                                "data/textures/terrain/ground_1.png", true );

    // 18 threshold
    Airport::createSegment( m_airportRunwayGroup,  1850.0,  1800.0, 0.0, "data/textures/runway/pa_threshold_bl.png" );

    // 18 taxiturn
    Airport::createSegment( m_airportRunwayGroup,  1800.0,  1750.0, 0.0, "data/textures/runway/pa_centerline.png" );

    // 18 designation
    createDigits( 1, 8,  1750.0,  1725.0 );
    Airport::createSegment( m_airportRunwayGroup,  1725.0,  1700.0, 0.0, "data/textures/runway/pa_designation_empty.png" );

    // 18 tc markings
    Airport::createSegment( m_airportRunwayGroup,  1700.0,  1550.0, 0.0, "data/textures/runway/pa_tz_three.png" );
    Airport::createSegment( m_airportRunwayGroup,  1550.0,  1400.0, 0.0, "data/textures/runway/pa_aim.png" );
    Airport::createSegment( m_airportRunwayGroup,  1400.0,  1250.0, 0.0, "data/textures/runway/pa_tz_two.png" );
    Airport::createSegment( m_airportRunwayGroup,  1250.0,  1100.0, 0.0, "data/textures/runway/pa_tz_two.png" );
    Airport::createSegment( m_airportRunwayGroup,  1100.0,   950.0, 0.0, "data/textures/runway/pa_tz_one.png" );
    Airport::createSegment( m_airportRunwayGroup,   950.0,   800.0, 0.0, "data/textures/runway/pa_tz_one.png" );

    // 18 taxiturn
    Airport::createSegment( m_airportRunwayGroup,   800.0,   750.0, 0.0, "data/textures/runway/pa_centerline_bl.png" );

    Airport::createSegment( m_airportRunwayGroup,
                                0.0, Airport::m_halfWidth, 750.0, -Airport::m_halfWidth,
                                "data/textures/runway/pa_centerline.png", true );
}

////////////////////////////////////////////////////////////////////////////////

void AirportRunway::createDigits( int digit1, int digit2, double x_from, double x_unto )
{
    double sign_L = 0.0;
    double sign_R = 0.0;

    if ( x_unto > x_from )
    {
        sign_L = -1.0;
        sign_R =  1.0;
    }
    else
    {
        sign_L =  1.0;
        sign_R = -1.0;
    }

    double ulx = x_unto;
    double uly = sign_L * Airport::m_halfWidth;
    double lrx = x_from;
    double lry = sign_R * Airport::m_halfWidth;

    // texture
    char digit1_temp[ 255 ];
    char digit2_temp[ 255 ];

    sprintf( digit1_temp, "%d", digit1 );
    sprintf( digit2_temp, "%d", digit2 );

    std::string baseString = "data/textures/runway/pa_";
    std::string textureFile1 = baseString + std::string( digit1_temp ) + std::string( "l.png" );
    std::string textureFile2 = baseString + std::string( digit2_temp ) + std::string( "r.png" );

    Airport::createSegment( m_airportRunwayGroup, ulx, uly, lrx, 0.0, textureFile1 );
    Airport::createSegment( m_airportRunwayGroup, ulx, 0.0, lrx, lry, textureFile2 );
}
